﻿using Dapper;
using ExcelConfig;
using System;

namespace cn.tozhang.linksms.data.datainit.db
{
    internal class init_runner
    {
        public static void CreateDB()
        {
            using var repo = new MysqlRepo(AppConfig.Text("数据库链接串"));

            repo.Open();
            var exist_message = repo.Connection.QueryFirstOrDefault<bool>("select 1 from INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA=@Schema and TABLE_NAME=@Name;", new { Schema = "sms", Name = "message" });
            if (!exist_message)
            {
                var lines = repo.Connection.Execute(
@"CREATE TABLE `sms`.`message` (
  `MsgID` CHAR(36) NOT NULL,
  `MsgFrom` NVARCHAR(1024) NOT NULL,
  `MsgTo` NVARCHAR(1024) NOT NULL,
  `MsgName` VARCHAR(512) NULL,
  `MsgTime` DATETIME NOT NULL,
  `MsgContent` LONGTEXT NULL,
  PRIMARY KEY (`MsgID`),
  INDEX `ix_time` (`MsgTime` ASC) INVISIBLE,
  INDEX `ix_to` (`MsgTo` ASC) INVISIBLE,
  INDEX `ix_from` (`MsgFrom` ASC) VISIBLE);");
            }
        }
    }
}
